<?php
/**
 * @package Admin,Mapper
 * @author Henrique Droog <henridroog@gmail.com>
 * @name UserMapper
 * @since 18/04/2011
 * @version 1.0
 */
class Admin_Model_UserMapper {

    protected $_dbTable;
    protected $_dbTableInfo;

    public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Admin_Model_DbTable_User');
        }
        return $this->_dbTable;
    }

    public function getDbTableInfo() {
        if (null === $this->_dbTable) {
            $this->_dbTableInfo = $this->getDbTable()->info();
        }else{
            $this->_dbTableInfo = $this->_dbTable->info();
        }
        return $this->_dbTableInfo;
    }

    public function save(Admin_Model_User $user) {
        $data = array(
            'user_name' => $user->getName(),
            'user_password' => $user->getPassword(),
            'user_dt_inicio' => "now()",
            'user_ativo' => $user->getAtivo(),
            'role_id' => $user->getRole(),
        );

        if ($user->getId()<=0) {
            unset($data['user_id']);
            $this->getDbTable()->insert($data);
        } else {
            unset($data['user_dt_inicio']);
            $this->getDbTable()->update($data, array('user_id = ?' => $user->getId()));
        }
    }

    public function find($id, Admin_Model_User $user) {
        $result = $this->getDbTable()->find($id['id']);
//        echo "<pre> Find:";
//        print_r($result);
//        echo "</pre>";
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $user->setId($row->user_id)
             ->setName($row->user_name)
             ->setInicio($row->user_dt_inicio)
             ->setAtivo($row->user_ativo)
             ->setRole($row->role_id);
        return $user;
    }

    public function fetchAll() {
        $select = $this->getDbTable()->select()
                ->setIntegrityCheck(false)
                ->from(array("u" => "acl.acl_user"))
                ->join(array("rl" => "acl.acl_role"),"u.role_id = rl.role_id",array("rl.role_name"));

        $resultSet = $this->getDbTable()->fetchAll($select);
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Admin_Model_User();
            $entry->setId($row->user_id)
                    ->setName($row->user_name)
                    ->setInicio($row->user_dt_inicio)
                    ->setAtivo($row->user_ativo)
                    ->setRole($row->role_name);
            $entries[] = $entry;
        }
        return $entries;
    }

    public function selecionaAll() {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Admin_Model_User();
            $entry->setId($row->user_id)
                    ->setName($row->user_name)
                    ->setInicio($row->user_dt_inicio)
                    ->setAtivo($row->user_ativo)
                    ->setRole($row->role_id);
            $entries[] = $entry;
        }
        return $entries;
    }
    //
    public function del(Admin_Model_User $user){
        $where = array("user_id = ?" =>$user->getId());
        $resultSet = $this->getDbTable()->delete($where);
        return $resultSet;
    }

}